import { Entity, Column, ManyToMany, Index } from 'typeorm';
import { BaseEntity } from '@app/common/entities/base.entity';

@Entity('tags')
@Index(['name'])
export class Tag extends BaseEntity {
  @Column({ type: 'varchar', length: 50 })
  name: string;

  @Column({ type: 'varchar', length: 7, default: '#1890ff' })
  color: string;

  @Column({ type: 'varchar', length: 255, nullable: true })
  description?: string;

  @Column({ type: 'int', default: 0 })
  count: number;

  @Column({ type: 'tinyint', default: 1, comment: '状态: 0=禁用, 1=启用' })
  status: number;

  @Column({ type: 'int', default: 0 })
  sort: number;

  @Column({ type: 'varchar', length: 100, nullable: true })
  slug?: string;

  // SEO 相关字段
  @Column({ type: 'varchar', length: 255, nullable: true })
  seoTitle?: string;

  @Column({ type: 'text', nullable: true })
  seoDescription?: string;

  @Column({ type: 'varchar', length: 500, nullable: true })
  seoKeywords?: string;

  // 博客关联
  @ManyToMany('Blog', 'tags')
  blogs: any[];

  // 虚拟属性：获取实际使用次数
  get actualCount(): number {
    return this.blogs?.length || 0;
  }

  // 虚拟属性：是否为热门标签（使用次数 > 5）
  get isPopular(): boolean {
    return this.actualCount > 5;
  }

  // 虚拟属性：获取标签级别（基于使用次数）
  get level(): 'low' | 'medium' | 'high' | 'hot' {
    const count = this.actualCount;
    if (count === 0) return 'low';
    if (count <= 3) return 'low';
    if (count <= 10) return 'medium';
    if (count <= 20) return 'high';
    return 'hot';
  }
}
